package com.example.xungeng2.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xungeng2.common.Result;
import com.example.xungeng2.pojo.RiskPoint;
import com.example.xungeng2.pojo.Userinfo;
import com.example.xungeng2.service.RiskPointService;
import com.example.xungeng2.service.UserinfoService;
import com.example.xungeng2.unknow.TableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@Slf4j
public class RiskPointController {

    @Autowired
    private RiskPointService riskPointService;

    @Autowired
    private UserinfoService userinfoService;

    @Cacheable(value = "riskPoint",key = "#limit")
    @GetMapping("/api/riskPoint/selectPage")
    public TableMap selectPageinfo(Long page, Long limit) {
        Page<RiskPoint> riskPointPage = riskPointService.selectAllPages(page, limit);
        TableMap tableMap = new TableMap();
        tableMap.setData(riskPointPage.getRecords());
        tableMap.setCount(riskPointPage.getTotal());
        return tableMap;
    }

    @CacheEvict(value = "riskPoint",allEntries = true)
    @PostMapping("/api/riskPoint/insert")
    public Result<String> insertRiskPoint(RiskPoint riskPoint) throws ParseException {
        System.out.println(riskPoint);
        RiskPoint riskPoint1 = new RiskPoint();
        riskPoint1.setHigh(3);
        riskPoint1.setZhong(2);
        riskPoint1.setLow(1);
        riskPoint1.setSum1(6);
        boolean flag = riskPointService.save(riskPoint1);
        if(flag){
            return Result.ok("添加成功");
        }else {
            return Result.fail("添加失败");
        }
    }

    @GetMapping("/api/riskpoint/selectAll")
    public Result selectAll(){
        List<RiskPoint> list = riskPointService.selectAllRiskPoint();
        return Result.ok(list);
    }


    @GetMapping("/api/getUserInfoList")
    public TableMap getUserList(Long page,Long limit){
        Page<Userinfo> page1 = userinfoService.pagelist(page,limit);
        TableMap tableMap = new TableMap();
        tableMap.setData(page1.getRecords());
        tableMap.setCount(page1.getTotal());
        return tableMap;
    }
}